<?php
/***
	客户联系记录表，包括：
	1. 根据客户ID查询联系记录 get_by_client($client_id)
	
**/
class Bid_sales_model extends CI_Model {
	
	public function __construct() {
		$this->load->database();
	}
	
	//由委托状态查询
	public function get_by_entrust ($entrust_status, $sale) {
		$query = $this->db->get_where('projects', array(
			'entrust_status' => $entrust_status,
			'sale' => $sale,
			'flag_del' => 0
		));
		return $query->result_array();
	}
	
	//由id查询
	public function get_by_id ($id) {
		$sqlstring = "select projects.*, users.name as sale_name from projects
			inner join users on projects.sale = users.id where projects.id = ".$id;
		$query = $this->db->query($sqlstring);
		return $query->row_array();
	}
	
	//下委托 - 根据project_id更新销售部分内容
	public function entrust ($id) {
		$entrust_date = date("Y-m-d");
		$data = array (
			'client' => $this->input->post('client'),
			'rfq_no' => $this->input->post('rfq_no'),
			'end_user' => $this->input->post('end_user'),
			'place' => $this->input->post('place'),
			'bid_type' => $this->input->post('bid_type'),
			'deadline' => $this->input->post('deadline'),
			'classification' => $this->input->post('classification'),
			'currency' => $this->input->post('currency'),
			'delivery_term' => $this->input->post('delivery_term'),
			'entrust_status' => 1,
			'entrust_date' => $entrust_date,
			'pr_status' => 0
		);
		
		$this->db->where('id', $id);
		$this->db->update('projects', $data);
	}
	
	//修改委托 - 根据project_id更新销售部分内容
	public function edit_entrust ($id) {
		$data = array (
			'client' => $this->input->post('client'),
			'rfq_no' => $this->input->post('rfq_no'),
			'end_user' => $this->input->post('end_user'),
			'place' => $this->input->post('place'),
			'bid_type' => $this->input->post('bid_type'),
			'deadline' => $this->input->post('deadline'),
			'classification' => $this->input->post('classification'),
			'currency' => $this->input->post('currency'),
			'delivery_term' => $this->input->post('delivery_term'),
		);
		
		$this->db->where('id', $id);
		$this->db->update('projects', $data);
	}
	
	
	/*
	 *拷贝过来的
	public function get_by_year ($year) {
		$sqlstring = "select projects.id, projects.code, projects.name, projects.flag_del, projects.sale, projects.entrust_status, users.name as sale_name from projects
			inner join users on projects.sale = users.id where code LIKE '%MP".$year."%' and flag_del = 0";
		$query = $this->db->query($sqlstring);
		return $query->result_array();
	}
	
	public function get_by_id ($id) {
		$sqlstring = "select projects.id, projects.code, projects.name, projects.flag_del, projects.sale, projects.entrust_status, users.name as sale_name from projects
			inner join users on projects.sale = users.id where projects.id = ".$id;
		$query = $this->db->query($sqlstring);
		return $query->row_array();
	}
	//新建
	public function create () {
		$data = array(
			'code' => $this->input->post('project_code'),
			'name' => $this->input->post('project_name'),
			'sale' => $this->input->post('sale'),
			'flag_del' => 0,
			'entrust_status' => 0
		);
		
		$this->db->insert('projects', $data);
	}
	
	//按id标记删除
	public function delete_by_id ($id) {
		$data = array (
			'flag_del' => 1
		);
		
		$this->db->where('id', $id);
		$this->db->update('projects', $data);
	}
	
	//根据id更新
	public function update_by_id ($id) {
		$data = array (
			'code' => $this->input->post('project_code'),
			'name' => $this->input->post('project_name'),
			'sale' => $this->input->post('sale')
		);
		
		$this->db->where('id', $id);
		$this->db->update('projects', $data);
	}
	*/
}
